package kin.json;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.jsontype.TypeSerializer;


//import kin.bean.ProductType;









public class FooJsonSerializer extends JsonSerializer<Object> {

 

	

	/*@Override
	public void serialize(ProductType arg0, JsonGenerator arg1,
			SerializerProvider arg2) throws IOException,
			JsonProcessingException {
		String value = null;
		Method m;
		try {
			String s=arg0.getClass().getSimpleName();
			m = arg0.getClass().getMethod("get"+s+"_name", new Class[]{});
			value=(String) m.invoke(arg0, null);
		} catch (SecurityException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		arg1.writeString(value);
	}
*/


	@Override
	public void serialize(Object arg0, JsonGenerator arg1,
			SerializerProvider arg2) throws IOException,
			JsonProcessingException {
		System.out.println("ssssssssss");
		Integer value =null;
		Method m;
		//System.out.println("id:");
		try {
			if(arg0.getClass().isEnum()){
				Enum ee=(Enum) arg0;
				value=ee.ordinal();
			}else{
			String s=arg0.getClass().getSimpleName();
			m = arg0.getClass().getMethod("get"+s+"_id", new Class[]{});
			value= (Integer) m.invoke(arg0, null);
			}
		} catch (SecurityException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	//arg1.writeNumberField("fgfdg", value);
	
	//arg1.writeFieldName("llll");
arg1.writeObject(value);
		//arg1.writeNumber(value);
		
	}

	@Override
	public void serializeWithType(Object value, JsonGenerator jgen,
			SerializerProvider provider, TypeSerializer typeSer)
			throws IOException, JsonProcessingException {
		System.out.println("sssss");
		super.serializeWithType(value, jgen, provider, typeSer);
	}

	
  
		
	

}

